const router = require('koa-router')();
const PTCore = require('pt-core-common');
const ptRequest = require('pt-core-common').rpc.request;
const config = require('../config/config.local');

const feeds = require('./feeds');
router.use('', feeds.routes(), feeds.allowedMethods());
// const feedsInventory = require('./___archived/feedsInventory');
// router.use('', feedsInventory.routes(), feedsInventory.allowedMethods());
const feedsAccountManagement = require('./feedsAccountManagement');
router.use('', feedsAccountManagement.routes(), feedsAccountManagement.allowedMethods());

// const feedsSubmissionResult = require('./___archived/feedsSubmissionResult');
// const feedsSubmissionListing = require('./___archived/feedsSubmissionListing');
// const feedsSubmission = require('./___archived/feedsSubmission');
// router.use('', feedsSubmissionResult.routes(), feedsSubmissionResult.allowedMethods());
// router.use('', feedsSubmissionListing.routes(), feedsSubmissionListing.allowedMethods());
// router.use('', feedsSubmission.routes(), feedsSubmission.allowedMethods());

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2! from pt-core-feeds'
  })
});

router.get('/ver', async (ctx, next) => {
  const deploymentVersion = config.ver;
  ctx.body = `welcome, deployment version: ${deploymentVersion}`;
});

router.get('/a', async (ctx, next) => {
  ctx.response.status = 200;
  ctx.response.body = {
    headers: ctx.request.headers,
    query: ctx.query,
    params: ctx.params
  };
  // await ctx.render('index', {
  //   title: JSON.stringify(headers, null, 2),
  //   data: headers
  // })
});

router.get('/e', async (ctx, next) => {
    try {
        const url = "http://NLB-pt-core-feeds-16e79bf617c389c2.elb.us-east-2.amazonaws.com:32781/json";
        const json = await ptRequest(url);
        console.log(json);
        ctx.response.status = 200;
        ctx.response.body = {
            data: json,
        };
    } catch (error) {
        ctx.response.status = 500;
        ctx.response.body = {
            data: error,
        };
    }
});

router.get('/d', async (ctx, next) => {
    console.log(PTCore.serviceDiscovery.Content["src-pt-core-products"].Instances[0].Attributes);
    const availableServiceInstance = PTCore.serviceDiscovery.Content["src-pt-core-products"].Instances[0];
    const ip = availableServiceInstance.Attributes["AWS_INSTANCE_IPV4"];
    const port = availableServiceInstance.Attributes["AWS_INSTANCE_PORT"]
    try {
        const url = "http://" + ip + ":" + port + "/json";
        console.log("sending request to " + url);
        const json =  await PTCore.request(url);
        console.log(json);
        ctx.response.status = 200;
        ctx.response.body = {
            data: json,
        };
    } catch (error) {
        ctx.response.status = 500;
        ctx.response.body = {
            data: error,
        };
    }
    // var params = {
    //     HealthStatus: "ALL",
    //     MaxResults: 10,
    //     NamespaceName: "pt",
    //     ServiceName: "src-pt-core-products",
    //     // Region: "us-east-2",
    // };
    // AWS.config.update({region: 'us-east-2'});
    // AWS.config.getCredentials(function(err) {
    //     if (err) console.log(err.stack);
    //     // credentials not loaded
    //     else {
    //         console.log("Region: ", AWS.config.region);
    //         console.log("Access key:", AWS.config.credentials.accessKeyId);
    //     }
    // });
    // var servicediscovery = new AWS.ServiceDiscovery();
    // servicediscovery.discoverInstances(params, function (err, data) {
    //     if (err) {
    //         console.log(err, err.stack);
    //     } // an error occurred
    //     else {
    //         console.log(data);
    //         console.log(data.Instances[0].Attributes);
    //         ctx.response.status = 200;
    //         ctx.response.body = {
    //             data: data,
    //         }           // successful response
    //     }
    // });
});

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
});

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
});

module.exports = router;
